Sometimes programmers desire to subdivide a single “machine word” into multiple variables, each with a unique label, so that the subdivisions can be referenced like any other variable. The subdivisions of the machine word are commonly called “bit-fields.” When the subdivided machine word is a memory space in system memory, or RAM, the compiler alone determines how the machine word is subdivided. However, when the subdivided machine word refers to hardware, such as a register in an integrated circuit (IC), it is the hardware design that determines the bit-field subdivision. Thus, the compiler's method of bit-field subdivision must match the hardware's method. Otherwise, software access to the hardware will read or write incorrect data. In some programming languages, such as C or C++, the method of bit-field subdivision is not standardized. In other words, each compiler vendor may implement bit-field subdivision in a different manner. Thus, using the actual bit-field technique of the programming language to read from or write to hardware (e.g. the registers in the IC) is unreliable and not portable between compilers from different vendors.
In the C programming language, bit-fields can only be applied to elements of a structure type, an example of which is shown in FIG. 1. In this example, a structure named “abc” 100 is defined to have three elements, or bit-fields 102, 104 and 106: a one-bit unsigned integer “a” 102, a two-bit unsigned integer “b” 104 and an eight-bit unsigned integer “c” 106.
Writing to and reading from the bit-fields is done using a conventional structure member operator, as shown in FIG. 2. For example, a write operation 108 writes an integer “value” to the bit-field b of the structure abc. A read operation 110 reads the contents of the bit-field b of the structure abc into an integer variable “readvalue.”
The bits in the machine word that get assigned to each bit-field is dependent on the implementation of the compiler software that compiles the program into machine readable format. One compiler, for example, may assign the bit-fields “a” 102, “b” 104 and “c” 106 from the least significant bit to the most significant bit, as illustrated by a machine word 112 shown in FIG. 3. Another compiler, however, may assign the bit-fields “a” 102′, “b” 104′ and “c” 106′ in the opposite order, as shown by another machine word 114. For physical bits or registers in the IC (not shown), however, the bits must correspond to the bit-fields 102, 104 and 106. Therefore, use of the compiler that assigns the bit-fields 102, 104 and 106 in the wrong order can cause errors in the operation of the IC when performing the write and read operations 108 and 110 (FIG. 2).
Typical work-arounds to this potential problem do not use actual bit-fields. Instead, they typically define “mask” and “shift” values for “pseudo” bit-fields of an unsigned integer variable. The pseudo bit-fields are isolated within the variable using AND, OR and SHIFT logical operators.
An example that implements the bit-fields 102–106 (FIGS. 1 and 3) in the machine word 112 (FIG. 3) as “pseudo” bit-fields in an unsigned integer variable “abc” 116 is shown in FIG. 4. “Define” statements 118, 120 and 122 define the “mask” values for the pseudo bit-fields a, b and c within the unsigned integer abc 116. The one-bit pseudo bit-field “a” occupies the bit-0 position in the machine word 112, so the mask value is 0001 (hex). The two-bit pseudo bit-field “b” occupies the bit-1 to bit-2 position in the machine word 112, so the mask value is 0006 (hex), as shown. The eight-bit pseudo bit-field “c” occupies the bit-3 to bit-10 position in the machine word 112, so the mask value is 07f8 (hex), as shown.
“Define” statements 124, 126 and 128 define the “shift” values for the pseudo bit-fields a, b and c. Since the pseudo bit-field “a” corresponds to the first bit (bit-0) of the machine word 112 (FIG. 3), pseudo bit-field “a” requires no shift value, i.e. the shift value is 0, as shown in define statement 124. Since the pseudo bit-field “b” starts after the first bit, pseudo bit-field “b” requires a shift value of 1, as shown in define statement 126. Since the pseudo bit-field “c” starts after the first three bits, pseudo bit-field “c” requires a shift value of 3, as shown in define statement 128.
A write operation 130, corresponding to the write operation 108 (FIG. 2), for writing a “value” into the pseudo bit-field “b” of the unsigned integer abc is shown in FIG. 5. Likewise, a read operation 132, corresponding to the read operation 110 (FIG. 2), for reading the current contents of the pseudo bit-field “b” into a variable “readvalue” is also shown.
An exemplary performance of the write operation 130 (FIG. 5) is illustrated by lines 134–144 in FIG. 6. The write operation 130 performs as a conventional “read-modify-write” operation. The current contents (line 134, all Y's denote retained values, 0 or 1) of the unsigned integer abc are read from the IC (not shown) and ANDed with the inverse of the “mask” value (line 136) for the pseudo bit-field “b” in order to zero-out the pseudo bit-field “b” (line 138). The “value” (line 140, e.g. 3, or 11 binary) is left-shifted by one bit (line 142) according to the “shift” value defined in define statement 126 (FIG. 4) and ORed with the zeroed-out unsigned integer abc (line 138) to insert the “value” into the pseudo bit-field “b” (line 144). The modified unsigned integer abc is written to the IC with only the pseudo bit-field “b” modified.
The pseudo bit-field technique requires the programmer to determine the “mask” and “shift” values, which is a time consuming and error prone process, especially when there may be hundreds of bit-fields, as is common in IC's today. Additionally, the resulting code is difficult to read and write. Writing the code is not intuitive to the programmer, so the technique is prone to coding errors.
It is with respect to these and other background considerations that the subject matter herein has evolved.